package work.tools.deadline
{
	import zf.data.formats.DateFormat;

	public class QualificationGroups
	{
		private var groups:Object;
		private var _startDate:Date;
		private var _openCount:uint;
		
		private static var instance:QualificationGroups;
		
		private var classNameList:Array=["QualificationEndDate","QualificationPassDay","QualificationOpenTotal"];
		public function QualificationGroups(date:Date=null)
		{
			if(instance)
			{
				throw new Error("This is Singleton.");
			}
			if(date)
			{
				_startDate=date;
			}
			
			if(!groups)
			{
				groups=new Object();
			}
			instance=this;
		}

		/**
		 * 打开次数 
		 * @return 
		 * 
		 */
		public function get openCount():uint
		{
			return _openCount;
		}

		public function set openCount(value:uint):void
		{
			_openCount = value;
		}

		/**
		 * 开始日期 
		 * @return 
		 * 
		 */
		public function get startDate():Date
		{
			return _startDate;
		}

		public function set startDate(value:Date):void
		{
			_startDate = value;
		}
		
		/**
		 * 获得一个单例 
		 * @return 
		 * 
		 */
		public static function getInstance():QualificationGroups
		{
			if(instance)
			{
				return instance;
			}
			return new QualificationGroups();
		}
		
		/**
		 * 是否过期 
		 * @return 
		 * 
		 */
		public function isPass():Boolean
		{
			if(new Date()<_startDate)
			{
				return true;
			}
			var iq:IQualification;
			for(var i:uint=0;i<classNameList.length;i++)
			{
				iq=IQualification(groups[classNameList[i]]);
				if(iq)
				{
					
					if(iq.execute()<0)
					{
						return true;
					}
				}
			}
			return false;
		}
		/**
		 * 获得剩余的期限天数 
		 * @return 如果返回正数，表示还有几天过期，如果返回负数，表示已经过期了几天
		 * 
		 */
		public function getSurplus():Number
		{
			var num:Number=(new Date().time-_startDate.time)/86400000;
			var minNum:Number=Number.MAX_VALUE;
			var newNum:Number;
			var iq:IQualification;
			for(var i:uint=0;i<classNameList.length;i++)
			{
				iq=IQualification(groups[classNameList[i]]);
				if(iq)
				{
					newNum=iq.execute();
					if(minNum>newNum)
					{
						minNum=newNum;
					}
				}
			}
			return minNum;
		}
		
		/**
		 * 添加一个期限条件 
		 * @param qualification 条件
		 */
		public function addQulification(qulifacation:IQualification):void
		{
			groups[qulifacation.getClassName()]=qulifacation;
		}
		
		/**
		 * 删除一个期限条件 
		 * @param className 期限条件类名
		 * @return 如果有返回这个期限条件，否则返回null
		 * 
		 */
		public function removeQulification(className:String):IQualification
		{
			var iq:IQualification=IQualification(groups[className]);
			groups[className]=null;
			delete groups[className];
			return iq;
		}
		
		/**
		 * 删除所有的期限条件 
		 * 
		 */
		public function removeAllQulification():void
		{
			groups=new Object();
		}
		
		/**
		 * 得到一个字符串 
		 * @return 
		 * 
		 */
		public function toString():String
		{
			var separator:String=":::::";
			var start:String=DateFormat.toDateTimeString(_startDate);
			
			var s:String=start+separator+_openCount;
			return s;
		}
		/**
		 * toString()后的字符串转 QualificationGroups
		 * @param s 通过toString()方法得到的字符串
		 * @return QualificationGroups
		 * 
		 */
		public static function StringToQualificationGroups(s:String):QualificationGroups
		{
			var separator:String=":::::";
			var arr:Array=s.split(separator);
			var date:Date=DateFormat.DateStringToDate(arr[0]);
			var oc:uint=uint(arr[1]);
			var qfg:QualificationGroups=QualificationGroups.getInstance();
			qfg.openCount=oc;
			qfg.startDate=date;
			
			return qfg;
		}
	}
}